<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_member_address_add_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
	    //var_dump($_SESSION); exit();
	
	    $result = array('status' => 0, 'message' => '', 'data' => array());
	
	    //var_dump($_POST); exit();	
	    $receive_province_id = init_user_data_key($_POST, 'province_id', 'str');
	    $receive_city_id = init_user_data_key($_POST, 'city_id', 'str');
	    $receive_district_id = init_user_data_key($_POST, 'district_id', 'str');
	    $receive_street_id = init_user_data_key($_POST, 'street_id', 'str', '0');
	    
	    $receive_address = init_user_data_key($_POST, 'receive_address', 'str');
	    $receiver = init_user_data_key($_POST, 'receiver', 'str');
	    $receive_mobile = init_user_data_key($_POST, 'receive_mobile', 'str');
	    $receive_tel = init_user_data_key($_POST, 'receive_tel', 'str');
	     
	    $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
	    $member_id = $session_member_info['id'];
	    
        $model_foreground_address_region = wwy_resource_container::get_model_foreground_address_region();
        //var_dump($model_foreground_address_region); exit();
        $check_region_result = $model_foreground_address_region->check_region($receive_province_id, $receive_city_id, $receive_district_id, $receive_street_id);
        //var_dump($check_region_result); exit();
        
        if($check_region_result['status'] < 0){
            $result['status']  = $check_region_result['status'];
            $result['message'] = $check_region_result['message'];
        }else if(empty($receive_address)){
            $result['status']  = -2;
            $result['message'] = '请输入收货地址';
        }else if(empty($receiver)){
            $result['status']  = -3;
            $result['message'] = '请输入收货人';
        }else if(empty($receive_mobile) && empty($receive_tel)){
            $result['status']  = -4;
            $result['message'] = '请输入手机或电话';
        }else{
            $model_foreground_member_address = wwy_resource_container::get_model_foreground_member_address();
            //var_dump($model_foreground_member_address); exit();
            
            $member_address_info = array(
                'member_id'   => $member_id,
                'province_id' => $receive_province_id,
                'city_id'     => $receive_city_id,
                'district_id' => $receive_district_id,
                'street_id'   => $receive_street_id,
                
                'address'     => htmlspecialchars($receive_address),
                'receiver'    => htmlspecialchars($receiver),
                'mobile'      => htmlspecialchars($receive_mobile),
                'tel'         => htmlspecialchars($receive_tel)
            );
            $add_result = $model_foreground_member_address->add($member_address_info);
            //var_dump($add_result); exit();
            
            if(!$add_result){
                $result['status']  = -5;
                $result['message'] = '新增失败';
            }else{
                $result['status']  = 1;
                $result['message'] = '新增成功';
            }
        }
        
        self::set_view_status($result['status']);
        self::set_view_message($result['message']);
    }
}